unit Formwait;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, ExtCtrls, Buttons, OleCtrls, SHDocVw, JvExControls,
  JvAnimatedImage, JvGIFCtrl;

type
  TfmFormWait = class(TForm)
    paMiddle: TPanel;
    laHeader: TLabel;
    pbFormWait: TProgressBar;
    bbCancel: TBitBtn;
    paLeft: TPanel;
    anFormRec: TAnimate;
    imgWait: TImage;
    JvGIFAnimator1: TJvGIFAnimator;
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bbCancelClick(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);

  private
    OldCur: TCursor;
    MC: boolean;
  public
    { Public declarations }
    thRpt: TThread;
  end;

procedure FormWaitStart(FormCaption, HeaderCaption: string);
procedure FormWaitFree;
procedure FormWaitRefresh;

var
  fmFormWait: TfmFormWait;
  ForegroundHWnd: HWND;
  WasEnabled: boolean;
  bUseGIF: boolean = false;

implementation


uses InvTypes, GIFImage;

{$R *.DFM}
{$R 'FormWait.res' 'FormWait.rc'}

procedure FormWaitStart(FormCaption, HeaderCaption: string);
begin
  bUseGIF := True;
  if fmFormWait = nil then
    fmFormWait := TfmFormWait.Create(Application);

  fmFormWait.Caption := FormCaption;
  fmFormWait.laHeader.Caption := HeaderCaption;

  if ForegroundHWnd <> 0 then
  begin
    WasEnabled := IsWindowEnabled(ForegroundHWnd);
    EnableWindow(ForegroundHWnd, false);
  end;

  fmFormWait.Show;
  fmFormWait.Update;

  fmFormWait.OldCur := Screen.Cursor;
  if HeaderCaption <> '' then
    Screen.Cursor := crHourglass
  else
    Screen.Cursor := crSQLWait;
  Application.ProcessMessages;
end;

procedure FormWaitRefresh;
var
  Msg: TMsg;
begin
  while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
  begin
    if Msg.Message <> WM_QUIT then
      if (Msg.Message = 0) or (Msg.Message = 15) then
      begin
        TranslateMessage(Msg);
        DispatchMessage(Msg);
      end;
  end;
end;

procedure FormWaitFree;
begin
  bUseGIF := false;
  if not Assigned(fmFormWait) then
    exit;

  if ForegroundHWnd <> 0 then
  begin
    EnableWindow(ForegroundHWnd, WasEnabled);
    ForegroundHWnd := 0;
  end;

  Screen.Cursor := fmFormWait.OldCur;

  fmFormWait.MC := true;
  fmFormWait.Free;
  fmFormWait := nil;

  Application.ProcessMessages;
end;

procedure TfmFormWait.FormShow(Sender: TObject);
begin
  if not bUseGIF then
    anFormRec.Active:= True;
end;

procedure TfmFormWait.FormHide(Sender: TObject);
begin
  if not bUseGIF then
    anFormRec.Active:= False;
end;

procedure TfmFormWait.FormCreate(Sender: TObject);
var
  gi: TGIFImage;
  rs: TResourceStream;
begin
  thRpt:= nil;
  MC := false;

  if bUseGIF then
  begin
    rs := TResourceStream.Create(hInstance, 'WAITGIF', RT_RCDATA);
    gi := TGIFimage.create;
    try
      gi.loadfromstream(rs);
      imgWait.Picture.Assign(gi);
    finally
      rs.free;
      gi.free;
    end;
    anFormRec.Hide;
    imgWait.Show;
  end
  else
  begin
    anFormRec.FileName:=AppPath + '\' + anFormRec.FileName;
  end;
end;

procedure TfmFormWait.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if thRpt <> nil then begin
    thRpt.Terminate;
    thRpt:= nil;
  end;
end;

procedure TfmFormWait.bbCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfmFormWait.FormDeactivate(Sender: TObject);
begin
{  if not MC then
  begin
    SetFocus;
    Update;
  end; }
end;

procedure TfmFormWait.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
//  CanClose := MC;
end;

end.
